##### ####################################################
#####                                               ######
#####       Calculate parliament glove vectors
#####                                               ######
##### ####################################################

# init ------------------------------------------------------------

rm(list=ls())
range01 <- function(x){(x-min(x))/(max(x)-min(x))}
trim <- function(s) gsub("^[[:space:]]+|[[:space:]]+$","",s)

# Load libraries

library(quanteda) # CRAN v3.0.0
library(quanteda.dictionaries) # [github::kbenoit/quanteda.dictionaries] v0.22
library(text2vec) # CRAN v0.6
set.seed(221186)

# Load data

load("working/debate_fcm.Rdata")

## Fit GLOVE model

glove = GlobalVectors$new(rank = 150, x_max = 2500L, learning_rate = .145)
debate_main = glove$fit_transform(debate_fcm, n_iter = 500, convergence_tol = 0.005, n_threads = 3)
  
debate_context = glove$components
  
word_vectors = debate_main + t(debate_context)

save(word_vectors, file = "working/word_vectors_150.Rdata")
  
